﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace jingziqi
{
    class Program
    {
        static void Main(string[] args)
        {
            char select = 'y';
            while (select == 'y'||select == 'Y')
            {
                char[,] sh = new char[3, 3];
                for (int i = 0; i < 3; i++)
                    for (int j = 0; j < 3; j++)
                        sh[i, j] = ' ';
                int count = 0;
                Console.WriteLine("游戏开始！");
                int flag = 0;
                while (flag == 0 && count < 9)
                {
                    Console.WriteLine("先手玩家请输入下棋位置(输入分两行，分别表示横纵坐标)：");
                    int a, b;
                    a = int.Parse(Console.ReadLine()) - 1;
                    b = int.Parse(Console.ReadLine()) - 1;
                    sh[a, b] = 'O';
                    count++;
                    Console.WriteLine("*-----*");
                    Console.WriteLine("|{0}|{1}|{2}|", sh[0, 0], sh[0, 1], sh[0, 2]);
                    Console.WriteLine("-------");
                    Console.WriteLine("|{0}|{1}|{2}|", sh[1, 0], sh[1, 1], sh[1, 2]);
                    Console.WriteLine("-------");
                    Console.WriteLine("|{0}|{1}|{2}|", sh[2, 0], sh[2, 1], sh[2, 2]);
                    Console.WriteLine("*-----*");
                    if (sh[0, 0] == sh[0, 1] && sh[0, 1] == sh[0, 2] && sh[0, 2] != ' ') flag = 1;
                    if (sh[1, 0] == sh[1, 1] && sh[1, 1] == sh[1, 2] && sh[1, 2] != ' ') flag = 1;
                    if (sh[2, 0] == sh[2, 1] && sh[2, 1] == sh[2, 2] && sh[2, 2] != ' ') flag = 1;
                    if (sh[0, 0] == sh[1, 0] && sh[1, 0] == sh[2, 0] && sh[2, 0] != ' ') flag = 1;
                    if (sh[0, 1] == sh[1, 1] && sh[1, 1] == sh[2, 1] && sh[2, 1] != ' ') flag = 1;
                    if (sh[0, 2] == sh[1, 2] && sh[1, 2] == sh[2, 2] && sh[2, 2] != ' ') flag = 1;
                    if (sh[0, 0] == sh[1, 1] && sh[2, 2] == sh[1, 1] && sh[1, 1] != ' ') flag = 1;
                    if (sh[0, 2] == sh[1, 1] && sh[2, 0] == sh[1, 1] && sh[1, 1] != ' ') flag = 1;
                    if (flag == 1)
                    {
                        Console.Write("先手玩家获胜！");
                        break;
                    }if (count == 9&&flag == 0)
                        break;
                    Console.WriteLine("后手玩家请输入下棋位置(输入为两行，分别表示横纵坐标)：");
                    a = int.Parse(Console.ReadLine()) - 1;
                    b = int.Parse(Console.ReadLine()) - 1;
                    sh[a, b] = 'X';
                    Console.WriteLine("-------");
                    Console.WriteLine("|{0}|{1}|{2}|", sh[0, 0], sh[0, 1], sh[0, 2]);
                    Console.WriteLine("|-|-|-|");
                    Console.WriteLine("|{0}|{1}|{2}|", sh[1, 0], sh[1, 1], sh[1, 2]);
                    Console.WriteLine("|-|-|-|");
                    Console.WriteLine("|{0}|{1}|{2}|", sh[2, 0], sh[2, 1], sh[2, 2]);
                    Console.WriteLine("-------");
                    if (sh[0, 0] == sh[0, 1] && sh[0, 1] == sh[0, 2] && sh[0, 2] != ' ') flag = 1;
                    if (sh[1, 0] == sh[1, 1] && sh[1, 1] == sh[1, 2] && sh[1, 2] != ' ') flag = 1;
                    if (sh[2, 0] == sh[2, 1] && sh[2, 1] == sh[2, 2] && sh[2, 2] != ' ') flag = 1;
                    if (sh[0, 0] == sh[1, 0] && sh[1, 0] == sh[2, 0] && sh[2, 0] != ' ') flag = 1;
                    if (sh[0, 1] == sh[1, 1] && sh[1, 1] == sh[2, 1] && sh[2, 1] != ' ') flag = 1;
                    if (sh[0, 2] == sh[1, 2] && sh[1, 2] == sh[2, 2] && sh[2, 2] != ' ') flag = 1;
                    if (sh[0, 0] == sh[1, 1] && sh[2, 2] == sh[1, 1] && sh[1, 1] != ' ') flag = 1;
                    if (sh[0, 2] == sh[1, 1] && sh[2, 0] == sh[1, 1] && sh[1, 1] != ' ') flag = 1;
                    if (flag == 1)
                    {
                        Console.Write("后手玩家获胜！");
                        break;
                    }
                    count++;
                }
                if (count == 9 && flag == 0)
                {
                    Console.WriteLine("和棋！");
                }
                Console.WriteLine("是否重新开始游戏？(y/n)");
                select = Convert.ToChar(Console.ReadLine());
            }
        }
    }
}